package com.example.algorithm_learning_platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.algorithm_learning_platform.common.Result;
import com.example.algorithm_learning_platform.entity.AlgorithmType;
import com.example.algorithm_learning_platform.service.AlgorithmTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "算法类型管理", description = "管理算法类型的基本信息")
@RestController
@RequestMapping("/api/algorithm-types")
@RequiredArgsConstructor
public class AlgorithmTypeController {

    private final AlgorithmTypeService algorithmTypeService;

    @Operation(summary = "创建算法类型")
    @PostMapping
    public Result<AlgorithmType> create(@RequestBody AlgorithmType algorithmType) {
        algorithmTypeService.save(algorithmType);
        return Result.success(algorithmType);
    }

    @Operation(summary = "获取所有算法类型")
    @GetMapping
    public Result<List<AlgorithmType>> listAll() {
        return Result.success(algorithmTypeService.list());
    }

    @Operation(summary = "获取算法类型详情")
    @GetMapping("/{id}")
    public Result<AlgorithmType> getById(@Parameter(description = "算法类型ID") @PathVariable Integer id) {
        return Result.success(algorithmTypeService.getById(id));
    }

    @Operation(summary = "更新算法类型")
    @PutMapping("/{id}")
    public Result<AlgorithmType> update(
            @Parameter(description = "算法类型ID") @PathVariable Integer id,
            @RequestBody AlgorithmType algorithmType) {
        algorithmType.setTypeId(id);
        algorithmTypeService.updateById(algorithmType);
        return Result.success(algorithmType);
    }

    @Operation(summary = "删除算法类型")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "算法类型ID") @PathVariable Integer id) {
        return Result.success(algorithmTypeService.removeById(id));
    }

    @Operation(summary = "分页查询算法类型")
    @GetMapping("/page")
    public Result<Page<AlgorithmType>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "算法类型名称") @RequestParam(required = false) String typeName) {
        
        LambdaQueryWrapper<AlgorithmType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(typeName != null, AlgorithmType::getTypeName, typeName);
        return Result.success(algorithmTypeService.page(new Page<>(page, size), queryWrapper));
    }
} 